var openid, zushu = 0,acidacid,sitesite,oldzushu2
var oldzushu=0,showcode = 0
var sdc222ode,lockType
var sdcode=2
var app = getApp()
Page({
  data: {
    arr:[],Avatar:app.globalData.userInfo.avatarUrl,acid:'',
    num:[1,2,3,4,5], num1:[6,7,8,9,10],  num2:[11,12,13,14,15],
    show:false,  dataset:-1,  zushu:'',  auto:true,
    cmm:'重命名',  sdfz:'锁定分组',canstart:false,starttimer:'',
    display:'none',  ss:false,  placename:'',  duigou:[],
    img:'https://www.juquduo.com/static/resourse/zjfz/suoding.png',  img1:'https://www.juquduo.com/static/resourse/zjfz/juxing1.png',
    lockType:'0',  activityId:"",btncolor:''
  },
//点击选择分组
  hidd:function(){
    var that=this;
      that.setData({
        show:true
      })
  },
  sd:function(){
    var that = this
    // 如果没有选组
    if(zushu =='0'){
      wx.showToast({
        title: '请先选择分组',
        icon:'none'
      })
      // 已经选组后，第一次点锁定
    }else if(showcode=='0'){
      this.setData({canstart:true})
      console.log('显示开始按钮')
      showcode =1
      // 定时器，轮询检测教练是否已点开始
      that.starttimer = setInterval(function(){
        wx.request({
          // url: 'https://www.juquduo.com/juquduo/activity/lock_group',
          // data:{
          //   activityId:acidacid,lockType:1,groupId:zushu,
          //   oldGroupId:oldzushu2,openid:openid},
          // success(res){
          url: 'https://www.juquduo.com/juquduo/join_activity/is_start?activityId=271',
          data:{ },
          success(res){
            console.log('3秒一次',res)
            // var sdcode = res.data.code
            // if(sdcode==0&&that.data.lockType==1){
            //   clearInterval(that.starttimer)
            //   wx.showToast({
            //     title: '教练已开始游戏，点击进入',
            //     icon:'none'
            //   })
            //   // that.setData({btncolor:'lightgreen'})
            // }}})
            sdcode = res.data
            if(sdcode=='1'&&that.data.lockType==1){
              clearInterval(that.starttimer)
              wx.showToast({
                title: '教练已开始游戏，点击进入',
                icon:'none'
              })
              wx.redirectTo({
                url: '../dgame1/dgame1?id='+ acidacid +'&site='+sitesite+'&groupid='+zushu+'&canshu='+zushu
              })
              // that.setData({btncolor:'lightgreen'})
            }}})
      },3000)
    }
    // var lockType;
    var that=this;
      if(that.data.show==false){
        lockType=1
      }else{
        lockType=0
        //未锁定，点之后锁定，改为解锁按钮，锁定功能，随时都可以锁定
        that.setData({
          show:false,
          cmm:'',
          ss:true,
          sdfz:'解锁分组',
          img:'https://www.juquduo.com/static/resourse/zjfz/jiesuo.png',
          img1:'https://www.juquduo.com/static/resourse/zjfz/juxing.png',
          lockType:'1'
        })
      }
      // console.log('lockType',lockType)
      // console.log('oldzushu',oldzushu2)
      console.log('newzushu',zushu)
      if(zushu!=0){
        wx.request({
          // lockType为0,点击后锁定分组
          url: 'https://www.juquduo.com/juquduo/activity/lock_group',
          data:{
            activityId:acidacid,
            lockType:lockType,
            groupId:zushu,
            oldGroupId:oldzushu2,
            openid:openid
          },
          success(res){
            console.log(res)
            sdcode = res.data.code
            console.log('sdcode',sdcode)
            if(sdcode==0&&lockType==1){
              console.log('教练已点开始')
              wx.showToast({
                title: '活动已开始，无法换组',
                icon:'none'
              })
            }else if(lockType==1){
              that.setData({//已经点了锁定，现在是解锁按钮，改为锁定按钮
                cmm:'重命名',
                ss:false,
                sdfz:'锁定分组',
                img:'https://www.juquduo.com/static/resourse/zjfz/suoding.png',
                img1:'https://www.juquduo.com/static/resourse/zjfz/juxing1.png',
                lockType:'0',
                show:true
              })
            }
          }
        })
      }

  },

//点击重命名
  name:function(){
    var that=this;
    console.log(2)
    if(that.data.cmm=='重命名'){
      that.setData({
        auto:false,
        cmm:'确定',
      })
    }else if(that.data.cmm=='确定'){
      that.setData({
        auto:true,
        cmm:'重命名',
      })
    }else{
      console.log(2)
    }
  },
// 点击获取选择的组数
  dianji:function(e){
    
    // console.log('旧的组数',oldzushu)
    oldzushu2 =oldzushu
    var that= this
    zushu = e.target.dataset.num
    // console.log('当前点击',zushu)
    this.setData({
      dataset:e.target.dataset.num-1,
      display:'block',
      zushu:e.target.dataset.num
    })
    wx.request({
      url:"https://www.juquduo.com/juquduo/activity/group_players",
      data:{
        activityId:acidacid,
        // openid:'hhs12345',
        openid:openid,
        groupId:zushu,
        lockType:that.data.lockType,
      },
      header:{
        'Content-type': 'application/json'
      },
      success(res){
        that.setData({
          arr:res.data,
        })
        console.log('本组组号',res.data.MyGroup[0].GroupId)
        oldzushu = res.data.MyGroup[0].GroupId
      }
    })
  },
  onLoad: function (options) {
    //设置默认活动id，拿到acid和site，传给下个页面
    acidacid = options.id
    sitesite = options.site
    if(acidacid==undefined){
      console.log('没有活动id，改为264')
      acidacid = 271
    }
    this.setData({acid:acidacid})

    openid = wx.getStorageSync('openid')
    
    var headimg = wx.getStorageSync('headimg')
    this.setData({Avatar:headimg.avatarUrl})

    if(openid==''||openid=='undefined'){
      console.log(openid,avatar)
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
    }
    var that=this;
    that.setData({
      placeName:options.placeName
    })
    //获取后台数据，返回值没有使用
    wx.request({
      url:"https://www.juquduo.com/juquduo/locations/released_events",
      data:{
        placeName:that.data.placeName
      },
      success(res){
        // console.log("请求成功",res)
      }
    }),
    //获取后台数据，本组玩家
    wx.request({
      url:"https://www.juquduo.com/juquduo/activity/group_players",
      data:{
        activityId:acidacid,
        // openid:'hhs12345',
        openid:openid,
        groupId:3,
        lockType:that.data.lockType,
      },
      header:{
        'Content-type': 'application/json'
      },
      success(res){
        // console.log(that.data.lockType)
        that.setData({
          arr:res.data,
        })
        console.log('本组玩家',res.data)
      }
    })
    
  },
  startgame(){
    if(sdcode==0){
      var that = this
      clearInterval(that.starttimer)
      wx.redirectTo({
        url: '../dgame1/dgame1?id='+ acidacid +'&site='+sitesite+'&groupid='+zushu+'&canshu='+zushu
      })
    } else{
      wx.showToast({
        title: '等待教练开始游戏',
        icon:'none'
      })
    }
    
    // clearInterval(that.starttimer)
    // wx.setStorageSync('groupid', zushu)
  },
  onPullDownRefresh(){
    var that = this
    var headimg = wx.getStorageSync('headimg')
    that.setData({Avatar:headimg.avatarUrl})
    //获取后台数据，本组玩家
    wx.request({
      url:"https://www.juquduo.com/juquduo/activity/group_players",
      data:{
        activityId:acidacid,
        // openid:'hhs12345',
        openid:openid,
        groupId:zushu,
        lockType:that.data.lockType,
      },
      header:{
        'Content-type': 'application/json'
      },
      success(res){
        // console.log(that.data.lockType)
        that.setData({
          arr:res.data,
        })
        console.log('本组玩家',res.data)
      }
    })
  }
})